﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.OleDb;
using System.Collections.ObjectModel;
using libConn;

namespace ApartmentSale
{
	public partial class AffirmanceRule : Window
	{
		public AffirmanceRule()
		{
			this.InitializeComponent();

		}

        private bool getData()
        {
            string errMessage = null;
            string str = "Select * From affirmance_rules";

            DataTable oleDT;
            oleDT = libConn.AccdbConn.query(str, ref errMessage);

            if (errMessage != null)
            {
                System.Windows.MessageBox.Show(
                    errMessage,
                    "An error occurred while reading the rules of affirmance",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Error);
                return false;
            }

            ObservableCollection<AffirmanceRuleView> affirmanceRules = new ObservableCollection<AffirmanceRuleView>();

            foreach (DataRow r in oleDT.Rows)
            {
                affirmanceRules.Add(new AffirmanceRuleView()
                {
                    ID = Convert.ToInt32(r[0]),
                    AffirmanceRuleType = r[1].ToString().Replace("''","'"),
                    AffirmanceRuleAmount = Convert.ToDouble(r[2]),
                    AffirmanceRuleRule = Convert.ToDouble(r[3])
                });
            }

            dataGrid.DataContext = affirmanceRules;

            return true;
        }

        private bool checkTypeUsing(AffirmanceRuleView data)
        {
            string errMessage = null;
            string str = "SELECT * FROM affirmance WHERE affirmance_rule_type = '"
                + data.AffirmanceRuleType.Replace("'", "''") + "'";

            DataTable oleDT;
            oleDT = libConn.AccdbConn.query(str, ref errMessage);

            if (errMessage != null)
            {
                System.Windows.MessageBox.Show(
                    errMessage,
                    "Remove faild",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Error);
                return false;
            }

            if (oleDT.Rows.Count > 0)
            {
                System.Windows.MessageBox.Show(
                    "The rules of affirmance have been used, first in the affirmance to modify the management of data, or can not be deleted.",
                    "Remove prohibited",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Stop);
                return false;
            }
            return true;
        }

        private void btAdd_Click(object sender, RoutedEventArgs e)
        {
            AffirmanceRuleInput dialog = new AffirmanceRuleInput();
            dialog.ShowDialog();
            getData();
        }

        private void btDelete_Click(object sender, RoutedEventArgs e)
        {
            if (dataGrid.SelectedIndex == -1)
                return;

            var affirmanceRuleView = dataGrid.SelectedItem as AffirmanceRuleView;
            if (affirmanceRuleView != null)
            {
                if (MessageBox.Show("Sure you want to delete this rule? \n This action can not be recovered!",
                    "Delete", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No)
                    return;

                string errMessage = null;
                string str = "SELECT * FROM affirmance WHERE affirmance_rule_type = '"
                    + affirmanceRuleView.AffirmanceRuleType.Replace("'", "''") + "'";

                DataTable oleDT;
                oleDT = libConn.AccdbConn.query(str, ref errMessage);

                if (errMessage != null)
                {
                    System.Windows.MessageBox.Show(
                        errMessage,
                        "Remove faild",
                        System.Windows.MessageBoxButton.OK,
                        System.Windows.MessageBoxImage.Error);
                    return;
                }

                if (oleDT.Rows.Count > 0)
                {
                    System.Windows.MessageBox.Show(
                        "The rules of affirmance have been used, first in the affirmance to modify the management of data, or can not be deleted.",
                        "Remove prohibited",
                        System.Windows.MessageBoxButton.OK,
                        System.Windows.MessageBoxImage.Stop);
                    return;
                }

                str = "DELETE FROM affirmance_rules WHERE affirmance_rule_type = '" +
                     affirmanceRuleView.AffirmanceRuleType.Replace("'", "''") + "'";

                libConn.AccdbConn.exec(str, ref errMessage);

                if (errMessage != null)
                {
                    System.Windows.MessageBox.Show(
                        errMessage,
                        "Remove faild",
                        System.Windows.MessageBoxButton.OK,
                        System.Windows.MessageBoxImage.Error);
                    return;
                }

                getData();
            }
        }
        
        private void btClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (!getData())
                this.Close();
        }
	}
}